﻿using Bussiness.BlogNews;
using Bussiness.Language;
using FOModels.BlogNews;
using FOModels.Language;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
namespace FO.Controllers
{
    public class BlogNewsController : Controller
    {
        //
        // GET: /BlogBlogNews/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult BlogNewsDetail(int BlogNewsID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BlogNewsBLL blognewsbll = new BlogNewsBLL();
            BlogNewsModels list = new BlogNewsModels();
            list = blognewsbll.getBlogNewsByIDToGet(BlogNewsID, lang.LangID);
            int viewcount = list.ViewCount + 1;
            blognewsbll.UpdateBlogNewsViewCount(viewcount, list.BlogNewsID);
            return View(list);
        }
        public ActionResult BlogNewsCate(int page, int BlogNewsCategoryID)
        {
            BlogNewsBLL bll = new BlogNewsBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BlogNewsModels blognews = bll.getTOP1BlogNewByCate(BlogNewsCategoryID, lang.LangID);
            if (blognews != null)
            {
                ViewBag.ListBlogNews = bll.getBlogNewByCateNotID(BlogNewsCategoryID, lang.LangID, blognews.BlogNewsID);
            }

            else
            {
                ViewBag.ListBlogNews = null;
            }
            return View(blognews);
        }
        public ActionResult getBlogNewsByHot()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BlogNewsBLL blognewsbll = new BlogNewsBLL();
            BlogNewsModels blognews = blognewsbll.getTOp1BlogNewsByBlogNewsHot(lang.LangID);
            if (blognews != null)
            {
                ViewBag.ListBlogNews = blognewsbll.getTOpBlogNewsByBlogNewsHotNotBlogNewsID(blognews.BlogNewsID, lang.LangID, 3);
            }
            else
            {
                ViewBag.ListBlogNews = null;
            }

            return PartialView(blognews);
        }
        public ActionResult getBlogNewsByID()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BlogNewsBLL blognewsbll = new BlogNewsBLL();
            BlogNewsModels blognews = blognewsbll.getTOp1BlogNewsByID(lang.LangID);
            if (blognews != null)
            {
                ViewBag.ListBlogNews = blognewsbll.getTOp3BlogNewsByIDNotID(lang.LangID, blognews.BlogNewsID);
            }
            else
            {
                ViewBag.ListBlogNews = null;
            }
            return PartialView(blognews);
        }
        public ActionResult getBlogNewsFeature()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BlogNewsBLL blognewsbll = new BlogNewsBLL();
            BlogNewsCategoryBLL blognewscatebll = new BlogNewsCategoryBLL();
            List<BlogNewsCategoryModels> blognewscatelist = blognewscatebll.ResposiBlogNewsCategoryIsNullEnable(lang.LangID);
            List<BlogNewsParentModels> listbn = new List<BlogNewsParentModels>();
            foreach (var b in blognewscatelist)
            {
                BlogNewsParentModels bn = new BlogNewsParentModels();
                bn.BlogNewsCategoryID = b.BlogNewsCategoryID;
                bn.BlogNewsCategoryName = b.BlogNewsCategoryName;
                bn.ListCate = blognewscatebll.ResposiBlogNewsCategoryManagerExViewByCate(b.BlogNewsCategoryID, lang.LangID);
                bn.BlogNewsItem = blognewsbll.getBlogNewsByParentID(1, b.BlogNewsCategoryID, lang.LangID);
                bn.ListNewsRelative = blognewsbll.getBlogNewsByParentIDNotBlogNewsID(4, b.BlogNewsCategoryID, lang.LangID, bn.BlogNewsItem.BlogNewsID);
                listbn.Add(bn);
            }
            return PartialView(listbn);
        }
        [Authorize(Roles = "8,9")]
        public ActionResult ManagerBlogNews()
        {
            return View();
        }
        [Authorize(Roles = "8,9")]
        public JsonResult getBlogNewsJson()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BlogNewsBLL blognewsbll = new BlogNewsBLL();
            List<BlogNewsModels> list = new List<BlogNewsModels>();
            list = blognewsbll.getall(lang.LangID);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        public ActionResult InsertBlogNews()
        {
            BlogNewsBLL blognewsbll = new BlogNewsBLL();
            BlogNewsCategoryBLL blognewscatebll = new BlogNewsCategoryBLL();
            LanguageBLL langbll = new LanguageBLL();
            List<BlogNewsCategoryModels> blognewscate = new List<BlogNewsCategoryModels>();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            blognewscate = blognewscatebll.ResposiBlogNewsCategoryManager(lang.LangID);
            ViewBag.BlogNewsCategoryList = new SelectList(blognewscate, "BlogNewsCategoryID", "BlogNewsCategoryName");

            return View();
        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertBlogNews(FO.Models.BlogNewsInsert card)
        {
            BlogNewsBLL blognewsbll = new BlogNewsBLL();
            BlogNewsCategoryBLL blognewscatebll = new BlogNewsCategoryBLL();
            LanguageBLL langbll = new LanguageBLL();
            List<BlogNewsCategoryModels> blognewscate = new List<BlogNewsCategoryModels>();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            blognewscate = blognewscatebll.ResposiBlogNewsCategoryManager(lang.LangID);
            ViewBag.BlogNewsCategoryList = new SelectList(blognewscate, "BlogNewsCategoryID", "BlogNewsCategoryName");
            if (ModelState.IsValid)
            {
                BlogNewsModels items = new BlogNewsModels();
                BlogNewsModels itemsLang = new BlogNewsModels();
                items.BlogNewsCategoryID = card.BlogNewsCategoryID;
                items.CreateUser = User.Identity.Name;
                items.Enable = false;
                if (card.BlogNewsImg == "")
                {
                    items.BlogNewsImg = "no-image-BlogNews.gif";
                }
                else
                {
                    items.BlogNewsImg = card.BlogNewsImg;
                }
                items.BlogNewsContent = WebUtility.HtmlEncode(card.BlogNewsContent);
                items.BlogNewsSumary = WebUtility.HtmlEncode(card.BlogNewsSumary);
                items.BlogNewsTitle = card.BlogNewsTitle;
                items.CreateUser = User.Identity.Name;
                items.BlogNewsHot = card.BlogNewsHot;
                ////
                itemsLang.BlogNewsCategoryID = card.BlogNewsCategoryID;
                itemsLang.CreateUser = User.Identity.Name;
                itemsLang.Enable = false;
                if (card.BlogNewsImg != "")
                {
                    itemsLang.BlogNewsImg = "no-image-BlogNews.gif";
                }
                else
                {
                    itemsLang.BlogNewsImg = card.BlogNewsImg;
                }
                itemsLang.BlogNewsContent = WebUtility.HtmlEncode(card.BlogNewsContentLang);
                itemsLang.BlogNewsSumary = WebUtility.HtmlEncode(card.BlogNewsSumaryLang);
                itemsLang.BlogNewsTitle = card.BlogNewsTitleLang;
                itemsLang.BlogNewsHot = card.BlogNewsHot;
                itemsLang.CreateUser = User.Identity.Name;
                blognewsbll.InsertBlogNews(items);
                BlogNewsModels carditem = blognewsbll.getTOP1();
                items.BlogNewsID = carditem.BlogNewsID;
                itemsLang.BlogNewsID = carditem.BlogNewsID;
                blognewsbll.InsertBlogNewsLanng(items, 1);
                blognewsbll.InsertBlogNewsLanng(itemsLang, 2);
                return RedirectToAction("ManagerBlogNews");
            }
            return View(card);
        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateBlogNews(int BlogNewsID)
        {
            BlogNewsCategoryBLL blognewscatebll = new BlogNewsCategoryBLL();
            BlogNewsBLL blognewsbll = new BlogNewsBLL();
            LanguageBLL langbll = new LanguageBLL();
            List<BlogNewsCategoryModels> blognewscate = new List<BlogNewsCategoryModels>();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            blognewscate = blognewscatebll.ResposiBlogNewsCategoryManager(lang.LangID);
            Models.BlogNews c = new Models.BlogNews();
            BlogNewsModels card = blognewsbll.getBlogNewsByID(BlogNewsID, lang.LangID);
            ViewBag.BlogNewsCategoryList = new SelectList(blognewscate, "BlogNewsCategoryID", "BlogNewsCategoryName", card.BlogNewsCategoryID);
            c.BlogNewsID = card.BlogNewsID;
            c.BlogNewsCategoryID = card.BlogNewsCategoryID;
            c.ViewCount = card.ViewCount;
            c.CreateDate = card.CreateDate;
            c.CreateUser = card.CreateUser;
            c.UpdateDate = card.UpdateDate;
            c.UpdateUser = card.UpdateUser;
            c.Enable = card.Enable;
            if (card.BlogNewsImg == "")
            {
                c.BlogNewsImg = "no-image-BlogNews.gif";
            }
            else
            {
                c.BlogNewsImg = card.BlogNewsImg;
            }
            c.BlogNewsTitle = card.BlogNewsTitle;
            c.BlogNewsHot = card.BlogNewsHot;
            c.BlogNewsSumary = WebUtility.HtmlDecode(card.BlogNewsSumary);
            c.BlogNewsContent = WebUtility.HtmlDecode(card.BlogNewsContent);
            return View(c);
        }
        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateBlogNews(FO.Models.BlogNews card)
        {

            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BlogNewsBLL blognewsbll = new BlogNewsBLL();
            BlogNewsCategoryBLL blognewscatebll = new BlogNewsCategoryBLL();
            List<BlogNewsCategoryModels> blognewscate = new List<BlogNewsCategoryModels>();
            BlogNewsModels ca = new BlogNewsModels();
            blognewscate = blognewscatebll.ResposiBlogNewsCategoryManager(lang.LangID);
            ca = blognewsbll.getBlogNewsByID(card.BlogNewsID, lang.LangID);
            ViewBag.BlogNewsCategoryList = new SelectList(blognewscate, "BlogNewsCategoryID", "BlogNewsCategoryName", card.BlogNewsCategoryID);
            if (ca != null)
            {
                ca.BlogNewsID = card.BlogNewsID;
                ca.BlogNewsCategoryID = card.BlogNewsCategoryID;
                ca.ViewCount = card.ViewCount;
                ca.CreateDate = card.CreateDate;
                ca.CreateUser = card.CreateUser;
                ca.UpdateUser = User.Identity.Name;
                ca.Enable = card.Enable;
                if (card.BlogNewsImg == "")
                {
                    ca.BlogNewsImg = "no-image-BlogNews.gif";
                }
                else
                {
                    ca.BlogNewsImg = card.BlogNewsImg;
                }
                ca.BlogNewsTitle = card.BlogNewsTitle;
                ca.BlogNewsHot = card.BlogNewsHot;
                ca.BlogNewsSumary = WebUtility.HtmlEncode(card.BlogNewsSumary);
                ca.BlogNewsContent = WebUtility.HtmlEncode(card.BlogNewsContent);
                blognewsbll.UpdateBlogNews(ca, lang.LangID);

                return RedirectToAction("ManagerBlogNews");
            }
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeleteBlogNews(int BlogNewsID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BlogNewsBLL bll = new BlogNewsBLL();
            BlogNewsModels card = new BlogNewsModels();
            card = bll.getBlogNewsByID(BlogNewsID, lang.LangID);
            if (card != null)
            {
                bll.DeleteBlogNews(card.BlogNewsID);
                var result = new { Status = "OK" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var result = new { Status = "Xóa thất bại" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }


        }

    }
}
